package demo2;

import java.util.Stack;
class Solution {
    //逆波兰表达式
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String x: tokens) {
            if(!isOpera(x)){
                //数字
                stack.push(Integer.parseInt(x));
            } else {
                //运算符
                int right = stack.pop();
                int left = stack.pop();
                switch(x) {
                    case "+":
                        stack.push(left + right);
                        break;
                    case "-":
                        stack.push(left - right);
                        break;
                    case "*":
                        stack.push(left * right);
                        break;
                    case "/":
                        stack.push(left / right);
                        break;
                    default:
                        System.out.println("运算符非法！");
                        break;
                }
            }
        }
        return stack.pop();
    }


    public boolean isOpera(String x) {
        if(x.equals("+") || x.equals("-") || x.equals("*") || x.equals("/")) {
            return true;
        }
        return false;
    }

    //括号匹配
    public boolean isVaild(String[] tokens) {
        Stack<String> stack = new Stack<>();
        for(String x : tokens) {
            if(x.equals("(") || x.equals("[") || x.equals("{")) {
                stack.push(x);
            } else if(x.equals(")") || x.equals("]") || x.equals("}")) {
                if(stack.isEmpty()) {
                    return false;
                }
                String pop = stack.pop();
                switch(x) {
                    case ")":
                        if (pop.equals("(")){
                            break;
                        } else {
                            return false;
                        }
                    case "]":
                        if (pop.equals("[")){
                            break;
                        } else {
                            return false;
                        }
                    case "}":
                        if (pop.equals("{")){
                            break;
                        } else {
                            return false;
                        }
                }
            } else {
                System.out.println("运算符错误！");
                return false;
            }
        }
        if(stack.isEmpty()) {
            return true;
        }
        return false;
    }
}

public class Test {
    public static void main(String[] args) {
        String[] tokens = {"10","6","9","3","+","-11","*","/","*","17","+","5","+"};
        String[] s1 = {"(",")","[","]","{","}"};
        String[] s2 = {"(","(",")","[","]","{","}"};
        String[] s3 = {"(",")","[","]","{","[","]","}"};
        String[] s4 = {"(",")","[","]","{","}","]"};
        String[] s5 = {"(",")","[","]","{","}","["};
        Solution solution = new Solution();
        System.out.println(solution.evalRPN(tokens));
        System.out.println(solution.isVaild(s1));
        System.out.println(solution.isVaild(s2));
        System.out.println(solution.isVaild(s3));
        System.out.println(solution.isVaild(s4));
        System.out.println(solution.isVaild(s5));
    }




    public static void main2(String[] args) {
        Mystack stack = new Mystack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println("==========");

    }

    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(12);
        stack.push(13);
        stack.push(14);

        int x = stack.pop();
        System.out.println(x);
    }
}
